package com.mhtool.core.lang;

import lombok.experimental.UtilityClass;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yin
 * @date 2021/12/27 17:20
 * @since 8.0.0
 */
@UtilityClass
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 下划线转驼峰法
     *
     * @param line
     *            源字符串
     * @param smallCamel
     *            大小驼峰,是否为小驼峰
     * @return 转换后的字符串
     */
    public static String underlineToCamel(String line, boolean smallCamel) {
        if (isEmpty(line)) {
            return EMPTY;
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(smallCamel && matcher.start() == 0 ? Character
                    .toLowerCase(word.charAt(0)) : Character.toUpperCase(word
                    .charAt(0)));
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }
    private static final Pattern humpPattern = Pattern.compile("[A-Z]");
    /**
     * 驼峰法转下划线
     *
     * @param line
     *            源字符串
     * @return 转换后的字符串
     */
    public static String camelToUnderline(String line) {
        if (isEmpty(line)) {
            return EMPTY;
        }
        Matcher matcher = humpPattern.matcher(line);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static boolean equalsIgnoreCase(String a, String b) {
        if (a == null) {
            return b == null;
        }
        return a.equalsIgnoreCase(b);
    }

    /**
     * Example: subString("abcd","a","c")="b"
     *
     * @param src
     * @param start
     *            null while start from index=0
     * @param to
     *            null while to index=src.length
     */
    public static String subString(String src, String start, String to) {
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.indexOf(to);
        if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
            return null;
        }

        if (null != start) {
            indexFrom += start.length();
        }

        return src.substring(indexFrom, indexTo);

    }

    /**
     * Example: subString("abcdc","a","c",true)="bcd"
     *
     * @param src
     * @param start
     *            null while start from index=0
     * @param to
     *            null while to index=src.length
     * @param toLast
     *            true while to index=src.lastIndexOf(to)
     * @return
     */
    public static String subString(String src, String start, String to,
                                   boolean toLast) {
        if (!toLast) {
            return subString(src, start, to);
        }
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.lastIndexOf(to);
        if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
            return null;
        }

        if (null != start) {
            indexFrom += start.length();
        }

        return src.substring(indexFrom, indexTo);

    }
    public static String utf8Str(byte[] data) {
        return str(data, StandardCharsets.UTF_8);
    }
    public static String str(byte[] data, Charset charset) {
        if (data == null) {
            return null;
        } else {
            return null == charset ? new String(data) : new String(data, charset);
        }
    }

    /**
     * 编码字符串，编码为UTF-8
     *
     * @param str 字符串
     * @return 编码后的字节码
     */
    public static byte[] utf8Bytes(CharSequence str) {
        return bytes(str, StandardCharsets.UTF_8);
    }
    /**
     * 编码字符串
     *
     * @param str     字符串
     * @param charset 字符集，如果此字段为空，则解码的结果取决于平台
     * @return 编码后的字节码
     */
    public static byte[] bytes(CharSequence str, Charset charset) {
        if (str == null) {
            return null;
        }

        if (null == charset) {
            return str.toString().getBytes();
        }
        return str.toString().getBytes(charset);
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////
    // String的原型链

    public static boolean startsWith(String src, String prefix) {
        return src != null && src.startsWith(prefix);
    }
    public static boolean endsWith(String src, String suffix) {
        return src != null && src.endsWith(suffix);
    }
    public static boolean contains(String src, CharSequence s) {
        return src != null && src.contains(s);
    }
    public static int length(String src) {
        return src != null ? src.length() : 0;
    }
}

